package org.c41.graphics.effect;

import org.c41.graphics.C41Graphic;
import org.c41.util.Check;

public abstract class AlphaEffect extends Effect {

	private long current_time;
	private float current_alpha;
	private final Paintable base;
	
	public AlphaEffect(Paintable base){
		Check.notNull(base);
		this.base=base;
		this.current_alpha=1.0f;
	}
	
	@Override
	public int getWidth() {
		return base.getWidth();
	}

	@Override
	public int getHeight() {
		return base.getHeight();
	}

	@Override
	public final void updateEffect(long during) {
		current_time+=during;
		this.current_alpha = updateAlpha(current_time);
	}
	
	protected abstract float updateAlpha(long current);

	@Override
	protected void complete() {
		super.complete();
		current_time=0L;
	}
	
	@Override
	public void paintCompleted(C41Graphic g) {
		base.paint(g);
	}

	@Override
	public void paintEffect(C41Graphic g) {
		float alpha_stack = g.getAlpha();
		g.setAlpha(current_alpha);
		base.paint(g);
		g.setAlpha(alpha_stack);
	}

	@Override
	public void paintReady(C41Graphic g) {
		base.paint(g);
	}

}
